#!/bin/sh

if [ -d /usr/local/lib/erlang/lib/kernel-2.11.4 ]
then
  echo Erlang R11B-4 already installed
else
  # this is the Erlang/OTP version to build
  OTP="R11B-4"

  checkdep () {
      $1 --help 2>/dev/null 1>/dev/null
      [ $? -eq 127 ] && return 1 || return 0
  }
  testdep () {
      checkdep $1 ; [ $? -eq 1 ] && {
          echo "**************** ERROR ***********************"
          echo "$1 does not seem to be installed"
          echo "you need the $1 command into your PATH"
          echo "**********************************************"
          exit -1
      }
  }

  findunzip () {
      zip=""
      checkdep gzip ; [ $? -eq 0 ] && zip="gzip -d"
      checkdep gunzip ; [ $? -eq 0 ] && zip="gunzip"
      echo $zip
  }
  findzip () {
      zip=""
      checkdep gzip ; [ $? -eq 0 ] && zip="gzip"
      checkdep bzip2 ; [ $? -eq 0 ] && zip="bzip2"
      echo $zip
  }
  findmake () {
      make=""
      checkdep make ; [ $? -eq 0 ] && make="make"
      checkdep gmake ; [ $? -eq 0 ] && make="gmake"
      echo $make
  }
  findcurl () {
      curl=""
      checkdep curl ; [ $? -eq 0 ] && curl="curl"
      checkdep wget ; [ $? -eq 0 ] && curl="wget -c"
      echo $curl
  }
  arch() {
      case `uname -m` in
      i[3456]86 ) echo x86 ;;
      i86pc) echo x86 ;;
      x86) echo x86 ;;
      x86_64) echo amd64 ;;
      ppc) echo powerpc ;;
      arm*b) echo armeb ;;
      arm*) echo arm ;;
      * ) echo `uname -p | tr [A-Z] [a-z]` ;;
      esac
  }

  # check dependencies
  testdep gcc
  testdep tar

  UNZIP=`findunzip`
  ZIP=`findzip`
  MAKE=`findmake`
  CURL=`findcurl`
  [ "$UNZIP" = "" ] && {
          echo "**************** ERROR ***********************"
          echo "gzip and gunzip don't seem to be installed"
          echo "you need an unzip command into your PATH"
          echo "**********************************************"
          exit -1
  }
  [ "$ZIP" = "" ] && {
          echo "**************** ERROR ***********************"
          echo "gzip and bzip2 don't seem to be installed"
          echo "you need a zip command into your PATH"
          echo "**********************************************"
          exit -1
  }
  [ "$MAKE" = "" ] && {
          echo "**************** ERROR ***********************"
          echo "make or gmake don't seem to be installed"
          echo "you need a make command into your PATH"
          echo "**********************************************"
          exit -1
  }
  [ "$CURL" = "" ] && {
          echo "**************** ERROR ***********************"
          echo "curl or wget don't seem to be installed"
          echo "you need a make command into your PATH"
          echo "**********************************************"
          exit -1
  }

  set -e

  # define archive name
  BASE="/tmp"
  [ ! -d "$BASE/archives" ] && {
    mkdir "$BASE/archives"
  }
  [ ! -d "$BASE/sources" ] && {
    mkdir "$BASE/sources"
  }
  os=`uname -s | tr [A-Z] [a-z]`
  cpu=`arch`
  echo $os | grep "^win" >/dev/null 2>&1 && AREF="windows" || AREF="$os-$cpu"

  # get archives
  cd $BASE/archives/
  $CURL http://www.erlang.org/download/otp_src_$OTP.tar.gz > $BASE/archives/otp_src_$OTP.tar.gz

  [ ! -d $BASE/sources/otp_src_$OTP ] && {
      (cd $BASE/sources; $UNZIP -c $BASE/archives/otp_src_$OTP.tar.gz | tar -xf -)
  }

  # configure and compile
  cd $BASE/sources/otp_src_$OTP
  ./configure --enable-threads --prefix=/usr/local
  $MAKE
  $MAKE install

  rm -rf $BASE/sources/otp_src_$OTP $BASE/archives/otp_src_$OTP.tar.gz
fi